package communityController;


import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import util.FileSaveUtil;
import vo.CommunityVo;
import dao.CommunityDao;

@Controller
public class CommunityBoardWrite {
	
	@Autowired
	CommunityDao cdao;

	@Autowired
	HttpServletRequest request;
	
	@RequestMapping(value="/project/community/board_write.do")
	public ModelAndView exe(CommunityVo cvo) throws Exception {
		
		
		
		
		String ip = request.getRemoteAddr();
		String filename= "no_file";
		String alias = (String) request.getSession().getAttribute("alias");

		
		MultipartFile file = cvo.getFile();
		if(!file.isEmpty()){
			String absPath=request.getServletContext().getRealPath("upload");
			filename=file.getOriginalFilename();
			filename=FileSaveUtil.saveFile(file, filename, absPath);
		}
		
		cvo.setB_ip(ip);
		cvo.setB_filename(filename);
		cvo.setB_writer(alias);
		System.out.println(alias);
		System.out.println(cvo.getTitle_no());
		System.out.println(cvo.getB_subject());
		System.out.println(cvo.getB_content());
		System.out.println(cvo.getB_writer());
		System.out.println(cvo.getB_ip());
		System.out.println(cvo.getB_filename());
		System.out.println(cvo.getMenu_no());
		System.out.println(cvo.getCate_no());
		System.out.println(cvo.getB_notice());
		
		
		cdao.insertCommunityBoard(cvo);
		
		ModelAndView mv = new ModelAndView();
		mv.addObject("menu_no", cvo.getMenu_no());
		mv.addObject("cate_no", cvo.getCate_no());
		mv.addObject("title_no", cvo.getTitle_no());
		mv.setViewName("redirect:/project/main/community_main.do?state=list");
		return mv;
	}
	
	@RequestMapping(value="/project/community/board_notice_write.do")
	public ModelAndView notice_exe(CommunityVo cvo) throws Exception {
		
		String ip = request.getRemoteAddr();
		String filename= "no_file";
		String alias = (String) request.getSession().getAttribute("alias");

	
		MultipartFile file = cvo.getFile();
		if(!file.isEmpty()){
			String absPath=request.getServletContext().getRealPath("upload");
			filename=file.getOriginalFilename();
			filename=FileSaveUtil.saveFile(file, filename, absPath);
		}
		
		cdao.updateNotice();
		cvo.setB_notice(1);
		cvo.setB_ip(ip);
		cvo.setB_filename(filename);
		cvo.setB_writer(alias);
		System.out.println(alias);
		System.out.println(cvo.getTitle_no());
		System.out.println(cvo.getB_subject());
		System.out.println(cvo.getB_content());
		System.out.println(cvo.getB_writer());
		System.out.println(cvo.getB_ip());
		System.out.println(cvo.getB_filename());
		System.out.println(cvo.getMenu_no());
		System.out.println(cvo.getCate_no());
		System.out.println(cvo.getB_notice());
		
		
		cdao.boardNotice(cvo);
		
		ModelAndView mv = new ModelAndView();
		mv.addObject("menu_no", cvo.getMenu_no());
		mv.addObject("cate_no", cvo.getCate_no());
		mv.addObject("title_no", cvo.getTitle_no());
		mv.setViewName("redirect:/project/main/community_main.do?state=notice");
		return mv;
	}

	
}
